package cn.kooki.app.duobao.ui.Activity.User;

import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;
import cn.kooki.app.duobao.data.Bean.User.pay.PayListResponse;
import cn.kooki.app.duobao.data.Bean.User.pay.PaylistItem;
import cn.kooki.app.duobao.data.bus.PayEvent;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.Activity.Pay.ReChargeActivity;
import cn.kooki.app.duobao.ui.Adapter.PayListAdapter;
import cn.kooki.app.duobao.ui.view.CustomRefreshHeader;
import cn.kooki.app.duobao.ui.view.LoadMoreContainer;
import cn.kooki.app.duobao.ui.view.LoadMoreHandler;
import cn.kooki.app.duobao.ui.view.LoadMoreListViewContainer;
import cn.kooki.app.duobao.ui.widget.CustomRelativeLayout;
import cn.kooki.app.duobao.util.PhoneCheckUtils;
import cn.kooki.app.duobao.util.SharePreferencesUtils;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/13.
 */
public class ChangeRecordActivity extends BaseNavActivity implements View.OnClickListener {


    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.load_more_content_container)
    LoadMoreListViewContainer loadMoreContentContainer;
    @Bind(R.id.swipe_refresh_layout)
    PtrFrameLayout swipeRefreshLayout;
    @Bind(R.id.loading_wrapper)
    CustomRelativeLayout loadingWrapper;
    private UserInfo userinfo;


    private ArrayList<PaylistItem> datas = new ArrayList<>();
    private PayListAdapter adapter;
    private int page = 1;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.activity_charge_record);
        ButterKnife.bind(this);
    }

    @Override
    protected void afterView() {
        setTitle(R.string.title_activity_charge_record);
        actionEdit.setText(R.string.user_charge);
        userinfo = SharePreferencesUtils.getUserinfo(this);
        actionEdit.setVisibility(View.VISIBLE);
        actionEdit.setTextColor(getResources().getColor(R.color.blue));
        actionEdit.setOnClickListener(this);
        loadingWrapper.showloading();
        adapter = new PayListAdapter(this, datas);
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        listView.setAdapter(adapter);
        loadMoreContentContainer.setAutoLoadMore(true);
        loadMoreContentContainer.setShowLoadingForFirstPage(true);
        loadMoreContentContainer.useDefaultFooter();
        CustomRefreshHeader refreshHeader = new CustomRefreshHeader(this);
        swipeRefreshLayout.addPtrUIHandler(refreshHeader);
        swipeRefreshLayout.setHeaderView(refreshHeader);
        swipeRefreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getdata(false);
            }
        });
        loadMoreContentContainer.setLoadMoreHandler(new LoadMoreHandler() {

            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                getdata(true);
            }
        });
        getdata(false);
    }

    private void getdata(final boolean isloadmore) {
        if (isloadmore) {
            page++;
        } else {
            page = 1;
        }
        MainFactory.getApiInstance().charerecordlist(userinfo.getUid(), page, new Callback<PayListResponse>() {
            @Override
            public void success(PayListResponse baseResponse, Response response) {
                if (baseResponse.data != null) {
                    if (!isloadmore) {
                        datas.clear();
                    }
                    datas.addAll(baseResponse.data);
                    adapter.notifyDataSetChanged();
                    loadMoreContentContainer.loadMoreFinish(datas.size() == 0, baseResponse.data.size() == Constants.DEFAULT_COUNT);
                }
                if (!isloadmore) {
                    hiderefresh();
                }

            }

            @Override
            public void failure(RetrofitError error) {
                hideloadmore();
                hiderefresh();
            }
        });
    }


    public void onEventMainThread(PayEvent event) {
        if (event.isSuccess && swipeRefreshLayout != null) {
            swipeRefreshLayout.autoRefresh(false);
        }
    }

    private void hiderefresh() {
        swipeRefreshLayout.refreshComplete();
        loadingWrapper.hideloading();
    }

    private void hideloadmore() {

    }

    @Override
    public void onClick(View v) {
        if (actionEdit == v) {
            if (PhoneCheckUtils.checkphone(this)) {
                intent(ReChargeActivity.class);
            }else {
                intent(BindActivity.class);
            }
        }
    }


}
